﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Threading;
using System.IO;

namespace Volume_Resource_Management
{
    public partial class frmSplash : Form
    {
        List<volumesPerDay> volumes = new List<volumesPerDay>();

        string storeNumber;
        int currentYear;
       
        public frmSplash()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            storeNumber = "RETGB261-WS0051";
            storeNumber = storeNumber.Substring(5, 3);
            //storeNumber = System.Environment.MachineName.Substring(5,3);
            currentYear = Convert.ToInt32(getIKEAYear().Substring(2,2));

            if (databaseAccessability.checkForDatabase() == true)
            {          
                this.Text = "Volume Resource Management";

                if (databaseAccessability.getDBValues(Convert.ToInt32(storeNumber)) == 0)
                {
                    DialogResult res = MessageBox.Show("Data For Your Store Cannot Be Found" + Environment.NewLine + "Is This The First Time The Software Has Been Loaded?", "", MessageBoxButtons.YesNo);

                    if (res == System.Windows.Forms.DialogResult.Yes)
                    {
                        frmFirstLoad fLoad = new frmFirstLoad(storeNumber, this, currentYear);
                        this.Hide();
                        fLoad.Show();
                        fLoad.BringToFront();
                    }
                    else
                    {
                        Application.Exit();
                    }
                }
                else
                {
                    progressBar1.Maximum = databaseAccessability.getDBValues(Convert.ToInt32(storeNumber));
                    progressBar1.Maximum += File.ReadLines(Application.StartupPath + "\\storeMaps\\" + storeNumber + ".txt.").Count();

                    label2.Text = "Loading Daily Deliveries...";

                    Thread loading = new Thread(load_Information);
                    loading.IsBackground = true;
                    loading.Start();
                }
            }
            else
            {
                MessageBox.Show("A Connection To The Server Could Not Be Established, Please Contact An Administrator", "Connection Error", MessageBoxButtons.OK);
                Application.Exit();
            }
        }

        public string getIKEAYear()
        {
            DateTime currentDate = DateTime.Today;
            //DateTime currentDate = Convert.ToDateTime("01/09/2013");

            string retDate = "";

            if ((currentDate.Month >= 1) && (currentDate.Month <= 8))
            {
                retDate = currentDate.Year.ToString();
            }
            else
            {
                retDate = (currentDate.AddYears(1).Year).ToString();
            }

            return retDate;
        }

        private void load_Information()
        {
            DateTime curDate = DateTime.Now;

            for (int i = 0; i < 28; i++)
            {
                volumesPerDay volume = new volumesPerDay();

                DateTime newDate = curDate.AddDays(i);
                string dte = newDate.ToString("dd-MMM-yyyy");

                List<double> HFBs = new List<double>();
                List<double> SS = new List<double>();
                List<double> FS = new List<double>();

                volume.setDate(dte);

                for (int j = 0; j < 35; j++)
                {
                    int use = j + 1;

                    if (j != 4)
                    {
                        if (j == 20)
                        {
                            use = 92;
                            HFBs = databaseAccessability.readVolumes(dte, "92", this);
                        }
                        else
                        {
                            HFBs = databaseAccessability.readVolumes(dte, use.ToString(), this);
                        }
                    }
                    volume.setHFB(HFBs, use.ToString());

                    SS = databaseAccessability.readSM(dte, "1");
                    FS = databaseAccessability.readSM(dte, "2");

                    volume.setSM(SS, FS);
                }
                volumes.Add(volume);

            }

            this.Invoke(new Action(() => label2.Text = "Loading Store Map..."));
            int[,] storeMap = mapManagement.loadMap(storeNumber, this);
            this.Invoke(new Action(() => label2.Text = "Please Wait..."));

            frmMain main = new frmMain(volumes, storeMap, storeNumber, currentYear);
            this.Invoke(new Action(() => main.Show()));
            this.Invoke(new Action(() => this.Hide()));
        }
    }
}
